﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using CommonShop.Service.MapClasses;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;

namespace CommonShop.Service.Helper
{
    public class NHibernateHelper
    {
        private ISessionFactory _sessionFactory;
        public NHibernateHelper() {
            _sessionFactory = GetSessionFactory();
        }
        private ISessionFactory GetSessionFactory() {
            return (new Configuration()).Configure().BuildSessionFactory();
        }
        public ISession GetSession() {
            return _sessionFactory.OpenSession();
        }

        public static ISessionFactory CreateSessionFactory() {
            return Fluently.Configure().Database(
               MySQLConfiguration
               .Standard
               .ConnectionString(c => c.FromConnectionStringWithKey("Conn")))
               .Mappings(m => m.FluentMappings.AddFromAssembly(Assembly.Load("CommonShop.Service")))
               //.ExposeConfiguration(CreateSchema)
               .BuildSessionFactory();
        }

        

        private static void CreateSchema(Configuration cfg) {
            var schemaExport = new SchemaExport(cfg);
            schemaExport.SetOutputFile("D:\\CodePlex\\CommonShop.Web\\2.sql");
            schemaExport.Create(false, true);
        }
    }
}